import 'dart:io';

import 'package:flutter_model_package/Employee.dart';
import 'package:flutter_model_package/user.dart';
import 'package:parse_server_sdk/parse_server_sdk.dart';

const String _keyTableName = 'Branch';

const String keyAddress = 'address';
const String keyAlbums = "albums";
const String keyAvatar = 'avatar';
const String keyBusinessHours = "businessHours";
const String keyContacts = 'contacts';
const String keyCreateAt = 'createAt';
const String keyDisplayName = 'displayName';
const String keyEmployees = "employees";
const String keyEnTitle = 'enTitle';
const String keyLocation = 'location';
const String keyNumber = 'number';
const String keyObjectId = 'objectId';
const String keyOldMySQLID = 'oldMySQLID';
const String keyOrder = 'order';
const String keyRemark = 'remark';
const String keySubTitle = 'subTitle';
const String keyUpdateAt = 'updatedAt';

class Branch extends ParseObject implements ParseCloneable {
  Branch() : super(_keyTableName);

  Branch.clone() : this();

  @override
  Branch clone(Map<String, dynamic> map) => Branch.clone()..fromJson(map);

  @override
  Branch fromJson(Map<String, dynamic> objectData) {
    super.fromJson(objectData);
    return this;
  }

  String get objectId => get<String>(keyObjectId);

  set objectId(String objectId) => set<String>(keyObjectId, objectId);

  DateTime get createAt => get<DateTime>(keyCreateAt);

  set createAt(DateTime createAt) => set<DateTime>(keyCreateAt, createAt);

  DateTime get updateAt => get<DateTime>(keyUpdateAt);

  set updateAt(DateTime updateAt) => set<DateTime>(keyUpdateAt, updateAt);

//  --------
  String get number => get<String>(keyNumber);

  set number(String number) => set<String>(keyNumber, number);

  String get displayName => get<String>(keyDisplayName);

  set displayName(String displayName) =>
      set<String>(keyDisplayName, displayName);

  String get subTitle => get<String>(keySubTitle);

  set subTitle(String subTitle) => set<String>(keySubTitle, subTitle);

  String get enTitle => get<String>(keyEnTitle);

  set enTitle(String enTitle) => set<String>(keyEnTitle, enTitle);

  File get avatar => get<File>(keyAvatar);

  set avatar(File avatar) => set<File>(keyAvatar, avatar);

  String get address => get<String>(keyAddress);

  set address(String address) => set<String>(keyAddress, address);

  ParseGeoPoint get location => get<ParseGeoPoint>(keyLocation);

  set location(ParseGeoPoint location) =>
      set<ParseGeoPoint>(keyLocation, location);

  List get contacts => get<List>(keyContacts);

  set contacts(List contacts) => set<List>(keyContacts, contacts);

  String get remark => get<String>(keyRemark);

  set remark(String remark) => set<String>(keyRemark, remark);

  num get order => get<num>(keyOrder);

  set order(num order) => set<num>(keyOrder, order);

  num get oldMySQLID => get<num>(keyOldMySQLID);

  set oldMySQLID(num oldMySQLID) => set<num>(keyOldMySQLID, oldMySQLID);

  QueryBuilder<Employee> relatedEmployee() {
    return QueryBuilder.or(Employee(), [
      QueryBuilder(Employee())..whereEqualTo("branch", this),
      QueryBuilder(Employee())..whereEqualTo("branches", this)
    ]);
  }

  QueryBuilder<User> relatedEmployeeUser() {
    return QueryBuilder(User("", "", ""))
      ..whereMatchesQuery("employee", this.relatedEmployee());
  }

  QueryBuilder<ParseSession> relatedEmployeeSession() {
    return QueryBuilder(ParseSession())
      ..whereMatchesQuery("user", this.relatedEmployeeUser())
      ..whereGreaterThan("expiresAt", DateTime.now());
  }

  QueryBuilder<ParseInstallation> relatedEmployeeInstallation() {
    return QueryBuilder(ParseInstallation())
      ..whereMatchesKeyInQuery(
          "installationId", "installationId", this.relatedEmployeeSession());
  }

  static QueryBuilder<Employee> EmptyBranchEmployee() {
    // return QueryBuilder(Emloyee())..whereDo
  }

  static QueryBuilder<User> EmptyBranchEmployeeUser() {
    return QueryBuilder(User("", "", ""))
      ..whereMatchesQuery("employee", Branch.EmptyBranchEmployee());
  }

  static QueryBuilder<ParseSession> EmptyBranchEmployeeUserSession() {
    return QueryBuilder(ParseSession())
      ..whereMatchesQuery("user", Branch.EmptyBranchEmployeeUser())
      ..whereGreaterThan("expiresAt", DateTime.now());
  }

  static QueryBuilder<ParseInstallation> EmptyBranchEmployeeInstallation() {
    return QueryBuilder(ParseInstallation())
      ..whereMatchesKeyInQuery("installationId", "installationId",
          Branch.EmptyBranchEmployeeUserSession());
  }

  QueryBuilder<Branch> searchBranch(List<QueryBuilder<Branch>> branchs){
    return QueryBuilder.or(this, branchs);
  }

}
